package com.qs.home.ui.activity;

import android.databinding.Observable;
import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.baidu.ocr.demo.FileUtil;
import com.baidu.ocr.demo.RecognizeService;
import com.baidu.ocr.sdk.exception.OCRError;
import com.baidu.ocr.ui.camera.CameraActivity;
import com.google.gson.Gson;
import com.qs.base.base.BaseMyActivity;
import com.qs.base.global.SPKeyGlobal;
import com.qs.base.router.RouterActivityPath;
import com.qs.home.BR;
import com.qs.home.R;
import com.qs.home.databinding.HomeCertificateRecognitionActivityBinding;
import com.qs.home.entity.TextResultEntity;
import com.qs.home.ui.VM.CertificateRecognitionViewModel;

import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 证件识别
 *
 * @Author ltzz
 * @Date 2019/8/13
 */
@Route(path = RouterActivityPath.Home.CERTIFICATE_RECOGNITION)
public class CertificateRecognitionActivity extends BaseMyActivity<HomeCertificateRecognitionActivityBinding, CertificateRecognitionViewModel> {
    @Override
    protected boolean statusBarDarkFont() {
        return true;
    }

    @Override
    protected View titleLayout() {
        return null;
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.home_certificate_recognition_activity;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding.head.setTitleLeftText("证件识别");
        binding.head.setAutoFinish(this);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //驾驶证识别
        viewModel.uc.photoDiscernUc.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {

                //驾驶证扫描
                RecognizeService.recDrivingLicense(CertificateRecognitionActivity.this, FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath(),
                        new RecognizeService.ServiceListener() {
                            @Override
                            public void onResult(String result) {
                                viewModel.dismissDialog();
                                KLog.e("驾驶证扫描：" + result);
                                //是否会员
                                boolean isOk = false;
                                if (!StringUtils.isTrimEmpty(SPUtils.getInstance().getString(SPKeyGlobal.USER_ID))) {
                                    //用户已登录
                                    //查看是否是会员
                                    String date = SPUtils.getInstance().getString(SPKeyGlobal.USER_DATE, "");
                                    if (!StringUtils.isTrimEmpty(date)) {
                                        if (Integer.parseInt(date) > 0) {
                                            isOk = true;
                                        }
                                    }
                                }
                                if (!isOk) {
                                    String seeNumberStr = SPUtils.getInstance().getString(SPKeyGlobal.SEE_NUMBER, "0");
                                    int x = Integer.parseInt(seeNumberStr);
                                    if (x <= 0) {
                                        ToastUtils.showShort("识别次数已用完");
                                        return;
                                    }
                                }
                                ARouter.getInstance().build(RouterActivityPath.Home.DRIVE_DETAILS_ACTIVITY).withString("textResult", result).navigation();
                            }

                            @Override
                            public void onError(OCRError err) {
                                KLog.e(err);
                                ToastUtils.showShort("请重试....");
                            }
                        });
            }
        });
    }
}
